package backend;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;


public class Connect {

	public Document getConection(String urlS) throws IOException, ParserConfigurationException, SAXException{
        URL url = new URL(urlS);
        URLConnection urlc = url.openConnection();
        urlc.setDoOutput(false);
        urlc.setAllowUserInteraction(false);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String str;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
            sb.append("\n");
        }
        in.close();
        
        String response = sb.toString();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(response));
        Document dom = db.parse(is);
        return dom;
	}
}
